package com.persion.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.persion.common.core.domain.AjaxResult;
import com.persion.common.core.redis.RedisCache;
import com.persion.common.utils.StringUtils;
import com.persion.domain.Activity;
import com.persion.domain.JoinAct;
import com.persion.dto.Params;
import com.persion.service.JoinActService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.concurrent.TimeUnit;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 活动参与表(JoinAct)表控制层
 *
 * @author WangFK
 * @since 2024-01-26 18:16:08
 */
@RestController
@Slf4j
@RequestMapping("/manage/join")
public class JoinActController {
    /**
     * 服务对象
     */
    @Resource
    private JoinActService joinActService;
    @Resource
    private RedisCache redisCache;

    @GetMapping("/list")
    public AjaxResult list(Params params){

        Page<JoinAct> actPage = new Page<>(params.pageNum, params.pageSize);
        LambdaUpdateWrapper<JoinAct> lqw = new LambdaUpdateWrapper<>();
        String search = params.getSearch();
        lqw.like(StringUtils.isNotEmpty(search),JoinAct::getOpPhone,search).or()
                .like(StringUtils.isNotEmpty(search),JoinAct::getOpName,search)
                .like(StringUtils.isNotEmpty(params.getCreateTime()),JoinAct::getCreateTime,params.getCreateTime())
                .eq(StringUtils.isNotEmpty(params.getCategoryId()),JoinAct::getActId,params.getCategoryId())
                .eq(StringUtils.isNotEmpty(params.getExamineStatus()),JoinAct::getExamineStatus,params.getExamineStatus());
        List<JoinAct> records = joinActService.page(actPage, lqw).getRecords();
        if (ObjectUtil.isEmpty(records)){
            return AjaxResult.warn("暂无信息");
        }

        return AjaxResult.success()
                .put("rows",records).put("total",records.size());
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult selectOne(@PathVariable Serializable id) {
        JoinAct byId = joinActService.getById(id);
        JoinAct joinAct = new JoinAct();
        joinAct.setJaId(byId.getJaId());
        joinAct.setExamineRemark(byId.getExamineRemark());
        joinAct.setExamineStatus(byId.getExamineStatus());
        return AjaxResult.success(joinAct);
    }

    /**
     * 修改数据
     *
     * @param joinAct 实体对象
     * @return 修改结果
     */
    @PutMapping
    public AjaxResult update(@RequestBody JoinAct joinAct) {
        if (StringUtils.isEmpty(joinAct.getExamineStatus())){
            return AjaxResult.error("请按规范填写");
        }
        return AjaxResult.success(joinActService.updateById(joinAct));
    }

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable Serializable id) {
        return AjaxResult.success(joinActService.removeById(id));
    }
}

